import time
from selenium.webdriver.common.by import By
from common.Utils import FroumDriver

class FroumSingUp:
    driver=""
    url=""
    def __init__(self):
        self.driver=FroumDriver.driver
        #添加 隐式等待
        self.driver.implicitly_wait(5)
        self.url="http://127.0.0.1:9580/sign-up.html"
        self.driver.get(self.url)

    # 封装 “输入框清空” 函数
    def InPutClear(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").clear()

    #测试 [注册成功] 用例
    def SingUpSucTest(self):
        time.sleep(2)
        #输入用户名、昵称、密码、确认密码，勾选同意条款进行注册
        self.InPutClear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("测试用户123")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("测试人1234")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        #屏幕截图
        FroumDriver.getScreenShot()
        # 验证注册成功后是否跳转到登录页面
        self.driver.find_element(By.CSS_SELECTOR, "body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")

    # 测试 [注册失败] 用例
    def SingUpFailTest(self):
        #（1）用户名不输入，输入昵称，密码，确认密码，勾选同意条款进行注册
        self.InPutClear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("测试人123")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect="用户名不能为空"
        text=self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(2) > div").text
        assert text==expect
        #屏幕截图
        time.sleep(1)
        FroumDriver.getScreenShot()

        #（2）昵称不输入，输入用户名，密码，确认密码，勾选同意条款进行注册
        self.InPutClear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("测试用户123")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect="昵称不能为空"
        text=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(3) > div").text
        assert text==expect
        time.sleep(1)
        FroumDriver.getScreenShot()

        #（3）密码不输入，输入用户名，昵称，确认密码，勾选同意条款进行注册
        self.InPutClear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("测试用户123")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("测试人123")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect1="密码不能为空"
        expect2="请检查确认密码"
        text1=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(4) > div > div").text
        text2=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div").text
        assert text1==expect1 and text2==expect2
        time.sleep(1)
        FroumDriver.getScreenShot()

        #（4）确认密码不输入，输入用户名，昵称，密码，勾选同意条款进行注册
        self.InPutClear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("测试用户123")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("测试人123")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        expect="请检查确认密码"
        text=self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(5) > div > div").text
        assert text==expect
        time.sleep(1)
        FroumDriver.getScreenShot()

        #（5）输入用户名，昵称，密码，确认密码不勾选同意条款进行注册
        self.InPutClear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("测试用户123")
        self.driver.find_element(By.CSS_SELECTOR, "#nickname").send_keys("测试人123")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordRepeat").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.driver.find_element(By.CSS_SELECTOR, "#policy").click()
        button=self.driver.find_element(By.CSS_SELECTOR, "#policy")
       #检查某个Web元素是否被选中，可以判断单选按钮是否处于选中状态
        if not button.is_selected():
            print("请勾选同意注册条款")
        time.sleep(1)
        FroumDriver.getScreenShot()



























